Meta = require '../meta/Meta'
SystemMeta = require '../meta/SystemMeta'

exports.gGetAllMeta = (next)->
    this.body = Meta.getAllMeta()
    yield next

exports.gSaveMeta = ->
    type = @params.type
    name = @params.name
    meta = @request.body

    if type == 'entity'
        yield from Meta.gSaveEntityMeta(name, meta)
    else if type == 'option'
        yield from Meta.gSaveOptionsMeta(name, meta)
    else
        return this.status = 400

    this.status = 204

exports.gRemoveMeta = ->
    type = @params.type
    name = @params.name

    if type == 'entity'
        yield from Meta.gRemoveEntityMeta(name)
    else if type == 'component'
        yield from Meta.gRemoveComponentMeta(name)
    else if type == 'option'
        yield from Meta.gRemoveOptionsMeta(name)
    else
        return this.status = 400

    this.status = 204

exports.gGetEmptyEntityMeta = ->
    e = {fields: {}, db: Meta.DB.mongo}
    SystemMeta.patchSystemFields(e)
    this.body = e
    yield return